//
//  WYTDiaryInfoSelectionView.m
//  Diary
//
//  Created by lanou3g on 15/11/3.
//  Copyright © 2015年 王艺拓. All rights reserved.
//

#import "WYTDiaryInfoSelectionView.h"
#import "WYTDevicesTool.h"
#import "WYTAlertView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

#define kbaseImageViewWidth kScreenWidth * 0.85
#define kbaseImageViewHeight kbaseImageViewWidth * 1.618
#define kbaseImageViewMinY kScreenHeight * 0.26

#define kWeatherCollectionViewMinY kbaseImageViewHeight * 0.1
#define kWeatherCollectionViewInsets kScreenWidth / 49
#define kWeatherCollectionViewItemSize (kbaseImageViewWidth - kWeatherCollectionViewInsets * 5) / 8
#define kWeatherCollectionViewWidth kbaseImageViewWidth / 7 * 4
#define kWeatherCollectionViewHeight kWeatherCollectionViewItemSize * 3 + kWeatherCollectionViewInsets * 4

#define kLineViewWidth kbaseImageViewWidth * 0.9
#define kClockImageViewSide kbaseImageViewHeight * 0.15

#define kLocationIconMinY kbaseImageViewHeight * 0.05
#define kLocationIconImageViewSide kbaseImageViewWidth * 0.07
#define kLocationTextFieldWidth kbaseImageViewWidth * 0.9
#define kLocationTextFieldHeight kbaseImageViewHeight * 0.05

#define kDatePickerViewHeight kbaseImageViewHeight * 0.18

@interface WYTDiaryInfoSelectionView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic, strong)UIView *baseBoardView;
@property (nonatomic, strong)UIImageView *mainWeatherIconImageView;
@property (nonatomic, strong)NSString *weatherIconName;
@property (nonatomic, assign)NSInteger weatherIconIndex;
@property (nonatomic, strong)UICollectionView *weatherCollectionView;

@property (nonatomic, strong)UITextField *yearTextField;
@property (nonatomic, strong)UITextField *monthTextField;
@property (nonatomic, strong)UITextField *dateTextField;
@property (nonatomic, strong)UITextField *hoursTextField;
@property (nonatomic, strong)UITextField *minutesTextField;

@property (nonatomic, strong)UILabel *dateLabel;
@property (nonatomic, strong)UILabel *timeLabel;



@property (nonatomic, strong)UIDatePicker *diaryDatePicker;

@property (nonatomic, strong)NSArray *whiteIconNameArray;
@property (nonatomic, strong)NSArray *grayIconNameArray;
@property (nonatomic, strong)NSArray *mainWhiteIconNameArray;

@property (nonatomic, strong)UIView *timeAlertViewA;
@property (nonatomic, strong)UIView *timeAlertViewB;

@end

@implementation WYTDiaryInfoSelectionView

-(instancetype)init{
    self = [super init];
    if (self) {
        [self setIconNameArray];
        [self setUps];
        self.backgroundColor = [UIColor clearColor];
        self.diaryInfoModel.weatherIconName = @"sunny_gray";
        self.weatherIconName = @"sunny_gray";
        self.diaryInfoModel.dateString = @" / / / : am";
    }
    return self;
}

- (void)setIconNameArray{
    self.whiteIconNameArray = [[NSArray alloc]init];
    self.grayIconNameArray = [[NSArray alloc]init];
    
    self.whiteIconNameArray = @[@"sunny_white", @"cloudy_white", @"overCast1_white", @"overCast2_white", @"lightRain_white", @"storm_white", @"heavyRain_white", @"thunder_white", @"windy_white", @"foggy_white", @"snowy_white", @"snowman_white"];
    self.grayIconNameArray = @[@"sunny_gray", @"cloudy_gray", @"overCast1_gray", @"overCast2_gray", @"lightRain_gray", @"storm_gray", @"heavyRain_gray", @"thunder_gray", @"windy_gray", @"foggy_gray", @"snowy_gray", @"snowman_gray"];
    self.mainWhiteIconNameArray = @[@"sunny_mainwhite", @"cloudy_mainwhite", @"overCast1_mainwhite", @"overCast2_mainwhite", @"lightRain_mainwhite", @"storm_mainwhite", @"heavyRain_mainwhite", @"thunder_mainwhite", @"windy_mainwhite", @"foggy_mainwhite", @"snowy_mainwhite", @"snowman_mainwhite"];
}

- (void)setUps{
    
    //UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    self.baseBoardView = [[UIView alloc]init];
    //self.baseBoardView.effect = blurEffect;
    self.baseBoardView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    //self.baseBoardView.alpha = 0;
    self.baseBoardView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.baseBoardView];
    
    self.baseImageView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - kbaseImageViewWidth) / 2, 736, kbaseImageViewWidth, kbaseImageViewHeight)];
    self.baseImageView.userInteractionEnabled = YES;
    self.baseImageView.image = [UIImage imageNamed:@"infoCardBG"];
    self.baseImageView.layer.shadowColor = [UIColor grayColor].CGColor;
    self.baseImageView.layer.shadowOffset = CGSizeMake(5, 5);
    self.baseImageView.layer.shadowOpacity = 0.8;
    [self.baseBoardView addSubview:self.baseImageView];
    
  
    
    UISwipeGestureRecognizer *swipeToJumpField = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeToJumpField:)];
    swipeToJumpField.direction = UISwipeGestureRecognizerDirectionRight;
    [self.baseImageView addGestureRecognizer:swipeToJumpField];
    
    UISwipeGestureRecognizer *swipeToJumpBack = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeToJumpBack:)];
    swipeToJumpBack.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.baseImageView addGestureRecognizer:swipeToJumpBack];
    
    self.mainWeatherIconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, kWeatherCollectionViewMinY, kbaseImageViewWidth - kWeatherCollectionViewWidth, kWeatherCollectionViewHeight)];
    self.mainWeatherIconImageView.image = [UIImage imageNamed:@"sunny_mainwhite"];
    self.weatherIconIndex = 0;
    self.mainWeatherIconImageView.backgroundColor = [UIColor clearColor];
    [self.baseImageView addSubview:self.mainWeatherIconImageView];
    
    UICollectionViewFlowLayout *weatherCollectionViewLayOut = [[UICollectionViewFlowLayout alloc]init];
    weatherCollectionViewLayOut.minimumInteritemSpacing = kWeatherCollectionViewInsets;
    weatherCollectionViewLayOut.minimumLineSpacing = kWeatherCollectionViewInsets;
    weatherCollectionViewLayOut.itemSize = CGSizeMake(kWeatherCollectionViewItemSize, kWeatherCollectionViewItemSize);
    weatherCollectionViewLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    weatherCollectionViewLayOut.sectionInset = UIEdgeInsetsMake(kWeatherCollectionViewInsets, kWeatherCollectionViewInsets, kWeatherCollectionViewInsets, kWeatherCollectionViewInsets);
    
    self.weatherCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(kbaseImageViewWidth - kWeatherCollectionViewWidth, kWeatherCollectionViewMinY, kWeatherCollectionViewWidth, kWeatherCollectionViewHeight) collectionViewLayout:weatherCollectionViewLayOut];
    self.weatherCollectionView.delegate = self;
    self.weatherCollectionView.dataSource = self;
    self.weatherCollectionView.backgroundColor = [UIColor clearColor];
    //注册
    [self.weatherCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"weatherIconCell"];
    [self.baseImageView addSubview:self.weatherCollectionView];
    
    //选择天气时,用于指示图标的lineView
    self.selectionLineView = [[UIView alloc]initWithFrame:CGRectMake(kWeatherCollectionViewInsets, kWeatherCollectionViewItemSize + kWeatherCollectionViewInsets, kWeatherCollectionViewItemSize, 1)];
    self.selectionLineView.backgroundColor = [UIColor whiteColor];
    self.selectionLineView.alpha = 0.5;
    [self.weatherCollectionView addSubview:self.selectionLineView];
    
    UIView *lineViewA = [[UIView alloc]initWithFrame:CGRectMake((kbaseImageViewWidth - kLineViewWidth) / 2, CGRectGetMaxY(self.weatherCollectionView.frame), kLineViewWidth, 1)];
    lineViewA.backgroundColor = [UIColor whiteColor];
    lineViewA.alpha = 0.5;
    [self.baseImageView addSubview:lineViewA];
    
    self.clockImageView = [[UIImageView alloc]initWithFrame:CGRectMake((kbaseImageViewWidth / 7 * 3 - kClockImageViewSide) / 2, CGRectGetMaxY(lineViewA.frame) + kScreenHeight * 0.02, kClockImageViewSide, kClockImageViewSide)];
    self.clockImageView.image = [UIImage imageNamed:@"clock"];
    //self.clockImageView.backgroundColor = [UIColor redColor];
    self.clockImageView.userInteractionEnabled = YES;
    [self.baseImageView addSubview:self.clockImageView];
    UITapGestureRecognizer *clockImageViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clockImageViewTap:)];
    [self.clockImageView addGestureRecognizer:clockImageViewTap];
    
    self.yearTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.weatherCollectionView.frame), CGRectGetMinY(self.clockImageView.frame) + kClockImageViewSide * 0.15, kWeatherCollectionViewWidth / 12 * 4, kClockImageViewSide * 0.4)];
    self.yearTextField.tag = 1101;
    self.yearTextField.placeholder = @"2015";
    self.yearTextField.textColor = [UIColor whiteColor];
    self.yearTextField.textAlignment = NSTextAlignmentCenter;
    self.yearTextField.delegate = self;
    self.yearTextField.keyboardType = UIKeyboardTypeDecimalPad;
    [self.baseImageView addSubview:self.yearTextField];
    
    UILabel *yearLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.yearTextField.frame), CGRectGetMinY(self.yearTextField.frame), kWeatherCollectionViewWidth / 12, CGRectGetHeight(self.yearTextField.frame))];
    yearLabel.text = @"/";
    yearLabel.textColor = [UIColor whiteColor];
    yearLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.baseImageView addSubview:yearLabel];
    
    self.monthTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(yearLabel.frame), CGRectGetMinY(self.yearTextField.frame), kWeatherCollectionViewWidth / 12 * 2, CGRectGetHeight(self.yearTextField.frame))];
    self.monthTextField.placeholder = @"11";
    self.monthTextField.textColor = [UIColor whiteColor];
    self.monthTextField.textAlignment = NSTextAlignmentCenter;
    self.monthTextField.delegate = self;
    self.monthTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.baseImageView addSubview:self.monthTextField];
    
    UILabel *monthLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.monthTextField.frame), CGRectGetMinY(self.yearTextField.frame), kWeatherCollectionViewWidth / 12, CGRectGetHeight(self.yearTextField.frame))];
    monthLabel.text = @"/";
    monthLabel.textColor = [UIColor whiteColor];
    monthLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.baseImageView addSubview:monthLabel];
    
    self.dateTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(monthLabel.frame), CGRectGetMinY(self.yearTextField.frame), kWeatherCollectionViewWidth / 12 * 2, CGRectGetHeight(self.yearTextField.frame))];
    self.dateTextField.placeholder = @"2";
    self.dateTextField.textAlignment = NSTextAlignmentCenter;
    self.dateTextField.textColor = [UIColor whiteColor];
    self.dateTextField.delegate = self;
    self.dateTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.baseImageView addSubview:self.dateTextField];
    
    self.dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.dateTextField.frame), CGRectGetMinY(self.yearTextField.frame), kWeatherCollectionViewWidth / 12 * 1 * 2, CGRectGetHeight(self.yearTextField.frame))];
    self.dateLabel.font = [UIFont boldSystemFontOfSize:16];
    if ([WYTDevicesTool iPhone320_DEVICES]) {
        self.dateLabel.font = [UIFont boldSystemFontOfSize:14];
    }
    self.dateLabel.textColor = [UIColor whiteColor];
    self.dateLabel.text = @"th";
    [self.baseImageView addSubview:self.dateLabel];
    
    self.hoursTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.monthTextField.frame), CGRectGetMaxY(self.monthTextField.frame), CGRectGetWidth(self.monthTextField.frame), CGRectGetHeight(self.monthTextField.frame))];
    self.hoursTextField.textAlignment = NSTextAlignmentCenter;
    self.hoursTextField.placeholder = @"18";
    self.hoursTextField.textColor = [UIColor whiteColor];
    self.hoursTextField.delegate = self;
    self.hoursTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.baseImageView addSubview:self.hoursTextField];
    
    UILabel *hoursLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.hoursTextField.frame), CGRectGetMaxY(yearLabel.frame), CGRectGetWidth(yearLabel.frame), CGRectGetHeight(yearLabel.frame))];
    hoursLabel.font = [UIFont boldSystemFontOfSize:16];
    hoursLabel.textColor = [UIColor whiteColor];
    hoursLabel.text = @":";
    [self.baseImageView addSubview:hoursLabel];
    
    self.minutesTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(hoursLabel.frame), CGRectGetMinY(self.hoursTextField.frame), CGRectGetWidth(self.dateTextField.frame), CGRectGetHeight(_dateLabel.frame))];
    self.minutesTextField.placeholder = @"47";
    self.minutesTextField.textColor = [UIColor whiteColor];
    self.minutesTextField.textAlignment = NSTextAlignmentCenter;
    self.minutesTextField.delegate = self;
    self.minutesTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.baseImageView addSubview:self.minutesTextField];
    
    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(_dateLabel.frame), CGRectGetMinY(self.minutesTextField.frame), CGRectGetWidth(_dateLabel.frame) * 2, CGRectGetHeight(self.minutesTextField.frame))];
    self.timeLabel.text = @"am";
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.font = [UIFont boldSystemFontOfSize:16];
    if ([WYTDevicesTool iPhone320_DEVICES]) {
        self.timeLabel.font = [UIFont boldSystemFontOfSize:14];
    }
    [self.baseImageView addSubview:self.timeLabel];
    UITapGestureRecognizer *tapToChangeTimeLabel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapToChangeTimeLabel:)];
    [self.timeLabel addGestureRecognizer:tapToChangeTimeLabel];
    
    UIButton *ampmSwitchButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.timeLabel.frame), CGRectGetMaxY(self.dateLabel.frame), CGRectGetWidth(self.timeLabel.frame) * 2, CGRectGetHeight(self.timeLabel.frame) * 2)];
    [ampmSwitchButton addTarget:self action:@selector(ampmSwitchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.baseImageView addSubview:ampmSwitchButton];
    
    UIView *lineViewB = [[UIView alloc]initWithFrame:CGRectMake((kbaseImageViewWidth - kLineViewWidth) / 2, CGRectGetMaxY(self.clockImageView.frame) + (CGRectGetMinY(self.clockImageView.frame) - CGRectGetMaxY(lineViewA.frame)), kLineViewWidth, 1)];
    lineViewB.backgroundColor = [UIColor whiteColor];
    lineViewB.alpha = 0.5;
    [self.baseImageView addSubview:lineViewB];
    
    self.locationIconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.clockImageView.frame), CGRectGetMaxY(lineViewB.frame) + (CGRectGetMinY(self.clockImageView.frame) - CGRectGetMaxY(lineViewA.frame)), CGRectGetWidth(self.clockImageView.frame), CGRectGetHeight(self.clockImageView.frame))];
    self.locationIconImageView.image = [UIImage imageNamed:@"location"];
    self.locationIconImageView.userInteractionEnabled = YES;
    [self.baseImageView addSubview:self.locationIconImageView];
    
    self.emptyLocationImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"emptyLocation"]];
    self.emptyLocationImageView.frame = CGRectMake(CGRectGetMaxX(self.locationIconImageView.frame), CGRectGetMinY(self.locationIconImageView.frame), kbaseImageViewWidth - CGRectGetMaxX(self.locationIconImageView.frame), CGRectGetHeight(self.locationIconImageView.frame) * 0.8);
    self.emptyLocationImageView.alpha = 0.1;
    [self.baseImageView addSubview:self.emptyLocationImageView];
    
    self.locationTextView = [[UITextView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.locationIconImageView.frame), CGRectGetMinY(self.locationIconImageView.frame) + CGRectGetHeight(self.locationIconImageView.frame) * 0.2, kWeatherCollectionViewWidth, CGRectGetHeight(self.locationIconImageView.frame) * 0.6)];
    self.locationTextView.textColor = [UIColor whiteColor];
    self.locationTextView.returnKeyType = UIReturnKeyDone;
    self.locationTextView.backgroundColor = [UIColor clearColor];
    self.locationTextView.textAlignment = NSTextAlignmentCenter;
    self.locationTextView.font = [UIFont systemFontOfSize:16];
    self.locationTextView.delegate = self;
    [self.baseImageView addSubview:self.locationTextView];
    
    self.mapBaseView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(lineViewA.frame), CGRectGetMaxY(self.locationIconImageView.frame), CGRectGetWidth(lineViewA.frame), kScreenHeight * 0.15)];
    self.mapBaseView.backgroundColor = [UIColor whiteColor];
    self.mapBaseView.layer.cornerRadius = 0.0187*kScreenWidth;
    [self.baseImageView addSubview:self.mapBaseView];
    
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(baseBlurViewHasBeenSwipeDown:)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [self.baseImageView addGestureRecognizer:swipeDown];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *weatherIconCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"weatherIconCell" forIndexPath:indexPath];
    
    UIImageView *iconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:self.whiteIconNameArray[indexPath.row]]];
    iconImageView.frame = CGRectMake(0, 0, kWeatherCollectionViewItemSize, kWeatherCollectionViewItemSize);
    iconImageView.userInteractionEnabled = YES;
    [weatherIconCell.contentView addSubview:iconImageView];
    return weatherIconCell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 12;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.mainWeatherIconImageView.image = [UIImage imageNamed:self.mainWhiteIconNameArray[indexPath.row]];
    //给属性赋值
    self.weatherIconIndex = indexPath.row;
    self.weatherIconName = self.grayIconNameArray[indexPath.row];
    [self moveSelectionLineViewTo:indexPath.row];
}

- (void)moveSelectionLineViewTo:(NSInteger)RowOfindexPath{
    switch (RowOfindexPath) {
        case 0:
            [self selectionLineViewAnimationToRowOfIndexPath:0 line:1];
            break;
            
        case 1:
            [self selectionLineViewAnimationToRowOfIndexPath:1 line:1];
            break;
            
        case 2:
            [self selectionLineViewAnimationToRowOfIndexPath:2 line:1];
            break;
            
        case 3:
            [self selectionLineViewAnimationToRowOfIndexPath:3 line:1];
            break;
            
        case 4:
            [self selectionLineViewAnimationToRowOfIndexPath:4 line:2];
            break;
            
        case 5:
            [self selectionLineViewAnimationToRowOfIndexPath:5 line:2];
            break;
            
        case 6:
            [self selectionLineViewAnimationToRowOfIndexPath:6 line:2];
            break;
            
        case 7:
            [self selectionLineViewAnimationToRowOfIndexPath:7 line:2];
            break;
            
        case 8:
            [self selectionLineViewAnimationToRowOfIndexPath:8 line:3];
            break;
            
        case 9:
            [self selectionLineViewAnimationToRowOfIndexPath:9 line:3];
            break;
            
        case 10:
            [self selectionLineViewAnimationToRowOfIndexPath:10 line:3];
            break;
            
        case 11:
            [self selectionLineViewAnimationToRowOfIndexPath:11 line:3];
            break;
            
        default:
            break;
    }
}

- (void)selectionLineViewAnimationToRowOfIndexPath:(NSInteger)RowOfIndexPath line:(NSInteger)line{
    if (3== 3) {
        [UIView animateWithDuration:0.3 animations:^{
            self.selectionLineView.frame = CGRectMake(kWeatherCollectionViewInsets * (RowOfIndexPath + 1) + kWeatherCollectionViewItemSize * RowOfIndexPath - (kWeatherCollectionViewItemSize * 4 + kWeatherCollectionViewInsets * 4) *(line - 1), kWeatherCollectionViewInsets + kWeatherCollectionViewItemSize * line + kWeatherCollectionViewInsets * (line - 1), kWeatherCollectionViewItemSize, 1);
        }];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([self.yearTextField isFirstResponder]) {
        [self.yearTextField resignFirstResponder];
        [self.monthTextField becomeFirstResponder];
    }else if([self.monthTextField isFirstResponder]){
        [self.monthTextField resignFirstResponder];
        [self.dateTextField resignFirstResponder];
    }else if([self.dateTextField isFirstResponder]){
        [self.dateTextField resignFirstResponder];
        [self.hoursTextField becomeFirstResponder];
    }else if([self.hoursTextField isFirstResponder]){
        [self.hoursTextField resignFirstResponder];
        [self.locationTextView becomeFirstResponder];
    }
    
    return YES;
}

- (void)baseBlurViewHasBeenSwipeDown:(UISwipeGestureRecognizer *)sender{
    
    BOOL keyboardIsOn = [self.locationTextView isFirstResponder] || [self.yearTextField isFirstResponder] || [self.monthTextField isFirstResponder] || [self.dateTextField isFirstResponder] || [self.hoursTextField isFirstResponder] || [self.minutesTextField isFirstResponder];
    
    if (keyboardIsOn) {
        
        [self.locationTextView resignFirstResponder];
        [self.yearTextField resignFirstResponder];
        [self.monthTextField resignFirstResponder];
        [self.dateTextField resignFirstResponder];
        [self.hoursTextField resignFirstResponder];
        [self.minutesTextField resignFirstResponder];
        
    }else{
        
        if ([self timeCorrect]) {
            //NSLog(@"该发通知让我下去了");
            [self setUpInfoModel];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"TAPTOJUMPOUTDIARYINFOVIEW" object:nil userInfo:nil];
            
//            self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight * 2);
            //NSLog(@"x === %f, y === %f, width === %f, height === %f", self.baseImageView.frame.origin.x, self.baseImageView.frame.origin.y, self.baseImageView.frame.size.width, self.baseImageView.frame.size.height);
            
            [UIView animateWithDuration:0.3 animations:^{
                
                self.baseImageView.frame = CGRectMake((kScreenWidth - kbaseImageViewWidth) / 2, 736, kbaseImageViewWidth, kbaseImageViewHeight);
                
            }];
            
        }else{
            
            //WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"点击钟表更正时间"];
            WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithClockSuggestionWithAlertLabelText:@"点击钟表更正时间"];
            [self.baseBoardView addSubview:alertView];
            
            self.timeAlertViewA = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.yearTextField.frame), CGRectGetMaxY(self.yearTextField.frame), CGRectGetWidth(self.weatherCollectionView.frame) / 12 * 10, 1)];
            [self.baseImageView addSubview:self.timeAlertViewA];
            self.timeAlertViewA.backgroundColor = [UIColor whiteColor];
            
            self.timeAlertViewB = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.yearTextField.frame), CGRectGetMaxY(self.hoursTextField.frame), CGRectGetWidth(self.weatherCollectionView.frame) / 12 * 10, 1)];
            [self.baseImageView addSubview:self.timeAlertViewB];
            self.timeAlertViewB.backgroundColor = [UIColor whiteColor];
            
            [UIView animateWithDuration:0.3 animations:^{
                self.timeAlertViewA.alpha = 0;
                self.timeAlertViewB.alpha = 0;
                self.clockImageView.alpha = 0;
                [UIView animateWithDuration:0.3 animations:^{
                    self.clockImageView.alpha = 1;
                }];
            }];
            
            [self performSelector:@selector(removeTimeAlertView) withObject:self afterDelay:0.31];
        }
    }
    
//    if (!(self.baseImageView.frame.origin.y == kbaseImageViewMinY)){
//        [UIView animateWithDuration:0.3f animations:^{
//            self.baseImageView.frame = CGRectMake(kbaseImageViewMinY, (kScreenHeight - kbaseImageViewHeight) / 2, kbaseImageViewWidth, kbaseImageViewHeight);
//        }];
//    }
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    [self.locationTextView resignFirstResponder];
    
    if (!(self.baseImageView.frame.origin.y == kbaseImageViewMinY)) {
        [UIView animateWithDuration:0.3f animations:^{
            self.baseImageView.frame = CGRectMake((kScreenWidth - kbaseImageViewWidth) / 2, kbaseImageViewMinY, kbaseImageViewWidth, kbaseImageViewHeight);
        }];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField.tag == 1101) {
        WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithTextFiledSuggestionWithAlertLabelText:@"轻扫输入下一个"];
        [self.baseBoardView addSubview:alertView];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}

- (void)swipeToJumpField:(UISwipeGestureRecognizer *)sender{
    if ([self.yearTextField isFirstResponder]) {
        [self.yearTextField resignFirstResponder];
        [self.monthTextField becomeFirstResponder];
    }else if([self.monthTextField isFirstResponder]){
        [self.monthTextField resignFirstResponder];
        [self.dateTextField becomeFirstResponder];
    }else if([self.dateTextField isFirstResponder]){
        [self.dateTextField resignFirstResponder];
        [self.hoursTextField becomeFirstResponder];
    }else if([self.hoursTextField isFirstResponder]){
        [self.hoursTextField resignFirstResponder];
        [self.minutesTextField becomeFirstResponder];
        [self ampmSwithing];
    }else if([self.minutesTextField isFirstResponder]){
        [self.minutesTextField resignFirstResponder];
        [self.locationTextView becomeFirstResponder];
    }
    
    [self setDateFormat];
}

- (void)swipeToJumpBack:(UISwipeGestureRecognizer *)sender{
    if ([self.yearTextField isFirstResponder]) {
        [self.yearTextField resignFirstResponder];
        [self setDateFormat];
    }else if([self.monthTextField isFirstResponder]){
        [self.monthTextField resignFirstResponder];
        [self.yearTextField becomeFirstResponder];
    }else if([self.dateTextField isFirstResponder]){
        [self.dateTextField resignFirstResponder];
        [self.monthTextField becomeFirstResponder];
        [self setDateFormat];
    }else if([self.hoursTextField isFirstResponder]){
        [self.hoursTextField resignFirstResponder];
        [self.dateTextField becomeFirstResponder];
        [self ampmSwithing];
    }else if([self.minutesTextField isFirstResponder]){
        [self.minutesTextField resignFirstResponder];
        [self.hoursTextField becomeFirstResponder];
    }
    
    [self setDateFormat];
}

- (void)tapToChangeTimeLabel:(UITapGestureRecognizer *)sender{
    //NSLog(@"timelabeltap");
    [self ampmSwithing];
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    [self.emptyLocationImageView removeFromSuperview];
    [UIView animateWithDuration:0.3f animations:^{
        
//        CGFloat yy = self.baseImageView.frame.origin.y;
        
        if (self.baseImageView.frame.origin.y == kbaseImageViewMinY) {
            CGFloat x = self.baseImageView.frame.origin.x;
            CGFloat y = self.baseImageView.frame.origin.y;
            CGFloat width  = self.baseImageView.frame.size.width;
            CGFloat height = self.baseImageView.frame.size.height;
            
            self.baseImageView.frame = CGRectMake(x, y - kScreenHeight * 0.45, width, height);
        }
        
        //不做这个iPhone4/4s调不出来
        if ([WYTDevicesTool iPhone4_iPhone4s]) {
            if (self.baseImageView.frame.origin.y < 20) {
                CGFloat x = self.baseImageView.frame.origin.x;
                CGFloat y = self.baseImageView.frame.origin.y;
                CGFloat width  = self.baseImageView.frame.size.width;
                CGFloat height = self.baseImageView.frame.size.height;
                
                self.baseImageView.frame = CGRectMake(x, y - kScreenHeight * 0.45, width, height);
            }
        }
        
    }];
}

- (void)ampmSwithing{
    if ([self.hoursTextField.text integerValue] > 12) {
        self.timeLabel.text = @"pm";
        NSInteger hour = [self.hoursTextField.text integerValue];
        self.hoursTextField.text = [NSString stringWithFormat:@"%ld", hour - 12];
    }
    
    if ([self.hoursTextField.text integerValue] == 12){
        self.timeLabel.text = @"pm";
    }
}

- (void)ampmSwitchButtonAction:(UIButton *)sender{
    if ([self.timeLabel.text isEqualToString:@"am"]) {
        self.timeLabel.text = @"pm";
    }else{
        self.timeLabel.text = @"am";
    }
}

- (void)setDateFormat{
    if ([self.dateTextField.text isEqualToString:@"1"] || [self.dateTextField.text isEqualToString:@"21"] || [self.dateTextField.text isEqualToString:@"31"]) {
        self.dateLabel.text = @"st";
    }else if([self.dateTextField.text isEqualToString:@"2"] || [self.dateTextField.text isEqualToString:@"22"]){
        self.dateLabel.text = @"nd";
    }else if([self.dateTextField.text isEqualToString:@"3"] || [self.dateTextField.text isEqualToString:@"23"]){
        self.dateLabel.text = @"rd";
    }else{
        self.dateLabel.text = @"th";
    }
    
    if ([self.yearTextField.text integerValue] <= 99 && self.yearTextField.text.length != 0) {
        NSString *originalString = self.yearTextField.text;
        //NSLog(@"NSString *originalString = self.dateTextField.text == %@", originalString);
        NSString *centryString = @"20";
        NSString *holeString = [centryString stringByAppendingString:originalString];
        self.yearTextField.text = holeString;
    }
    
    if ((([self.hoursTextField.text integerValue] < 10 && [self.hoursTextField.text integerValue] > 0) || [self.hoursTextField.text isEqualToString:@"0"]) && self.hoursTextField.text.length < 2) {
        NSString *originalString = self.hoursTextField.text;
        //NSLog(@"NSString *originalString = self.dateTextField.text == %@", originalString);
        NSString *centryString = @"0";
        NSString *holeString = [centryString stringByAppendingString:originalString];
        self.hoursTextField.text = holeString;
    }
    
    if ((([self.minutesTextField.text integerValue] < 10 && [self.minutesTextField.text integerValue] > 0) || [self.minutesTextField.text isEqualToString:@"0"]) && self.minutesTextField.text.length < 2 ) {
        NSString *originalString = self.minutesTextField.text;
        //NSLog(@"NSString *originalString = self.dateTextField.text == %@", originalString);
        NSString *centryString = @"0";
        NSString *holeString = [centryString stringByAppendingString:originalString];
        self.minutesTextField.text = holeString;
    }
}

- (BOOL)timeCorrect{
    
    BOOL monthCorrect = [self.monthTextField.text integerValue] <= 12 && [self.monthTextField.text integerValue] > 0;
    BOOL dateCorrect = [self.dateTextField.text integerValue] <= 31 && [self.monthTextField.text integerValue] > 0;
    BOOL hourCorrect = [self.hoursTextField.text integerValue] < 24 && [self.hoursTextField.text integerValue] >= 0;
    BOOL minutesCorrect = [self.minutesTextField.text integerValue] < 60 && [self.minutesTextField.text integerValue] >= 0;
    
    if (monthCorrect && dateCorrect && hourCorrect && minutesCorrect) {
        return YES;
    }else{
        return NO;
    }
}

- (void)removeTimeAlertView{
    [self.timeAlertViewA removeFromSuperview];
    [self.timeAlertViewB removeFromSuperview];
}

- (void)setUpInfoModel{
    self.diaryInfoModel = [[WYTDiaryInfoModel alloc]init];
    
    self.diaryInfoModel.weatherIconName = self.weatherIconName;
    self.diaryInfoModel.weatherIconIndex = self.weatherIconIndex;
    
    self.diaryInfoModel.timeString = [NSString stringWithFormat:@"%@/%@/%@ %@:%@%@", self.yearTextField.text, self.monthTextField.text, self.dateTextField.text, self.hoursTextField.text, self.minutesTextField.text, self.timeLabel.text];
    self.diaryInfoModel.yearString = self.yearTextField.text;
    self.diaryInfoModel.monthString = self.monthTextField.text;
    self.diaryInfoModel.dateString = self.dateTextField.text;
    self.diaryInfoModel.hourString = self.hoursTextField.text;
    self.diaryInfoModel.minutesString = self.minutesTextField.text;
    self.diaryInfoModel.locationString = self.locationTextView.text;
    
    
    if ([self.timeLabel.text isEqualToString:@"am"]) {
        self.diaryInfoModel.inTheMornig = YES;
    }else{
        self.diaryInfoModel.inTheMornig = NO;
    }
    
    self.diaryInfoModel.weatherIconName = self.weatherIconName;
    
    self.diaryInfoModel.locationString = self.locationTextView.text;
}

- (void)clockImageViewTap:(UITapGestureRecognizer *)sender{
    NSDate *currentTime = [NSDate date];
    
    [currentTime descriptionWithLocale:NSLocaleCountryCode];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
    
    NSRange yearRange = NSMakeRange(0, 4);
    NSRange monthRange = NSMakeRange(4, 2);
    NSRange dateRange = NSMakeRange(6, 2);
    NSRange hourRange = NSMakeRange(8, 2);
    NSRange minuteRange = NSMakeRange(10, 2);
    
    NSString *yearString = [currentTimeString substringWithRange:yearRange];
    NSString *monthString = [currentTimeString substringWithRange:monthRange];
    NSString *dateString = [currentTimeString substringWithRange:dateRange];
    NSString *hourString = [currentTimeString substringWithRange:hourRange];
    NSString *minutesString = [currentTimeString substringWithRange:minuteRange];
    
    self.yearTextField.text = yearString;
    self.monthTextField.text = monthString;
    self.dateTextField.text = dateString;
    self.hoursTextField.text = hourString;
    self.minutesTextField.text = minutesString;
    
    if ([self.hoursTextField.text integerValue] > 12) {
        self.timeLabel.text = @"pm";
    
        int currentHour = [hourString intValue];
        int currentHourpm = currentHour - 12;
        if (currentHourpm < 10) {
            self.hoursTextField.text = [NSString stringWithFormat:@"0%d", currentHourpm];
        }else{
            self.hoursTextField.text = [NSString stringWithFormat:@"%d", currentHourpm];
        }
    }
    
    if ([self.hoursTextField.text integerValue] == 12) {
        self.timeLabel.text = @"pm";
    }
}

@end
